// ## 搜索页
<template>
  <div class="searchs">
    <div class="top">
      <img class="search_icon" src="../../images/icon/search.png" />
      <input
        type="text"
        v-model="text"
        @blur="getSearchList"
        placeholder="输入关键字搜索"
      />
      <span class="no" @click="toIndex">取消</span>
    </div>
    <div class="center">
      <p>热门搜索</p>
    </div>
    <div class="bottom">
      <div class="textTop">
        <p>搜索历史</p>
        <img src="../../images/icon/clear-his.png" alt="" @click="delAll" />
      </div>
      <div class="searchText" v-for="(item, index) in arr" :key="index">
        <span @click="getListText(item)">{{ item }}</span>
      </div>
    </div>
  </div>
</template>

<script>
import { mapMutations, mapActions } from "vuex";

export default {
  data() {
    return {
      text: "", //输入的内容
      arr: [], //存放历史记录
    };
  },
  mounted() {
    this.arr = JSON.parse(wx.getStorageSync("info"));
  },

  methods: {
    ...mapMutations("search", ["getText"]),
    ...mapActions("search", ["getSearchInfo"]),
    //跳转到搜索页
    getSearchList() {
      //输入框不为空的时候跳转
      if (this.text !== " " && this.text !== "" && this.text !== null) {
        //将输入过的内容存放在数组里面
        this.arr.unshift(this.text.trim());
        wx.setStorageSync("info", JSON.stringify(this.arr));
        this.getSearchInfo(this.text.trim()); //调用异步方法 渲染出列表
        //跳转到搜索展示页
        wx.navigateTo({
          url: "../search-proad-show/index?text=" + this.text,
        });
      }
    },
    //点击取消，返回上一页
    toIndex() {
      wx.navigateBack(1);
    },
    //点击删除图标
    delAll() {
      //清空本地储存的所有数据
      wx.clearStorage("info");
      this.arr = [];
    },
    //点击历史记录的某一个按钮
    getListText(item) {
      //跳转到搜索展示页
      wx.navigateTo({
        url: "../search-proad-show/index?text=" + item,
      });
      //将输入的内容存在vuex里面
      this.getSearchInfo(item);
    },
  },
};
</script>

<style scoped>
.search {
  width: 100%;
}
.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 70rpx;
  margin: auto;
  width: 100%;
}
.top img.search_icon {
  width: 32rpx;
  height: 32rpx;
  position: absolute;
  left: 56rpx;
  top: 18rpx;
  display: block;
}
.top input {
  border-radius: 50rpx;
  background: #f7f7f7;
  width: 80%;
  text-align: center;
  font-size: 28rpx;
  margin: 0 20px;
  height: 90%;
  /* padding-top: 4rpx; */
}
.no {
  font-size: 28px;
  margin-right: 40px;
  color: #eb2444;
}
.center {
  min-height: 160rpx;
  border-bottom: 1px solid #eee;
}
.center p {
  font-size: 30rpx;
  padding: 30rpx 0;
  margin-top: 30px;
  margin-left: 20px;
  color: #666;
}
.bottom p {
  color: #666;
  margin-left: 20px;
  padding: 30rpx 0;
  margin-top: 30px;
  font-size: 30rpx;
}
.bottom span {
  max-width: 300rpx;
  overflow: hidden;
  float: left;
  border-radius: 50rpx;
  white-space: nowrap;
  text-overflow: ellipsis;
  background-color: #f2f2f2;
  box-sizing: border-box;
  margin-left: 20rpx;
  margin-right: 20rpx;
  margin-bottom: 20rpx;
  padding: 7rpx 22rpx;
  font-size: 30rpx;
}
.textTop {
  display: flex;
  justify-content: space-between;
}
.textTop img {
  width: 35px;
  height: 35px;
  margin-top: 60px;
  margin-right: 30px;
}
.del {
  margin-right: 20rpx;
}
</style>
